/**
 * 
 */
package cn.simonfish.acus.common.json;

import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author Simon Yu
 *
 */
public class JSONResult implements Result {
	
	private final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	private final static String DEFAULT_CHARSET = "UTF-8";
	private final static String DEFAULT_CONTENTTYPE = "text/html";
	
	private String root;
	
	private String format = DEFAULT_DATE_FORMAT;
	
	private String contentType = DEFAULT_CONTENTTYPE;
	
	private String charset = DEFAULT_CHARSET;
	

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
	 */
	public void execute(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest)actionContext.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
		HttpServletResponse response = (HttpServletResponse)actionContext.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
		Object object = findRootObject(invocation);
		SerializeConfig config = new SerializeConfig();
		
		config.put(java.util.Date.class,  new SimpleDateFormatSerializer(format));
		String json = JSON.toJSONString(object,config);
		response.setContentType(contentType);
		response.setCharacterEncoding(charset);
		Writer writer = response.getWriter();
		writer.write(json);
		writer.flush();
		
	}
	
	private Object findRootObject(ActionInvocation invocation)
    {
        Object rootObject;
        if(root != null)
        {
            ValueStack stack = invocation.getStack();
            rootObject = stack.findValue(root);
        } else
        {
            rootObject = invocation.getStack().peek();
        }
        return rootObject;
    }

	/**
	 * @return the root
	 */
	public String getRoot() {
		return root;
	}

	/**
	 * @param root the root to set
	 */
	public void setRoot(String root) {
		this.root = root;
	}

	/**
	 * @return the format
	 */
	public String getFormat() {
		return format;
	}

	/**
	 * @param format the format to set
	 */
	public void setFormat(String format) {
		this.format = format;
	}

	/**
	 * @return the contentType
	 */
	public String getContentType() {
		return contentType;
	}

	/**
	 * @param contentType the contentType to set
	 */
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	/**
	 * @return the charset
	 */
	public String getCharset() {
		return charset;
	}

	/**
	 * @param charset the charset to set
	 */
	public void setCharset(String charset) {
		this.charset = charset;
	}

}
